/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections;

using BlindShark.GameElements;

using SdlDotNet.Input;

namespace BlindShark
{
	/// <summary>
	/// Description of PlayerManager.
	/// </summary>
	public sealed class PlayerManager
	{
		#region StaticElements
		private static PlayerManager instance = new PlayerManager();
		
		public static PlayerManager Instance {
			get {
				return instance;
			}
		}
		
		#region GeneralFunctions
		public static void AddPlayer(Player p)
		{
			Instance.players.Add(p);
		}
		
		public static void RemovePlayer(int index)
		{
			Instance.players.RemoveAt(index);
		}
		#endregion
		
		#region PropertiesFunction
		public static void SetScore(int index, int score)
		{
			((Player)Instance.players[index]).Score = score;
		}
		
		public static int GetScore(int index)
		{
			return ((Player)Instance.players[index]).Score;
		}
		
		public static string GetName(int index)
		{
			return ((Player)Instance.players[index]).Name;
		}
		
		public static string GetDescription(int index)
		{
			return ((Player)Instance.players[index]).Description;
		}
		
		public static string GetAvatar(int index)
		{
			return ((Player)Instance.players[index]).Avatar;
		}
		
		public static void SetState(int index, PlayerState p)
		{
			((Player)Instance.players[index]).State = p;
		}
		
		public static PlayerState GetState(int index)
		{
			return ((Player)Instance.players[index]).State;
		}
		
		public static void SetKey(int index, Key k)
		{
			((Player)Instance.players[index]).BuzzerKey= k;
		}
		
		public static Key GetKey(int index)
		{
			return ((Player)Instance.players[index]).BuzzerKey;
		}
		
		public static Player GetPlayer(int index)
		{
			return (Player) Instance.players[index];
		}
		#endregion
		
		public static int Count
		{
			get {return Instance.players.Count;}
		}
		
		public static int CountPlaying
		{
			get
			{
				int count = 0;
				foreach(Player p in Instance.players)
				{
					if (p.State == PlayerState.inGame)
						count++;
				}
				return count;
			}
		}
		
		public static int Answering
		{
			get
			{
				int r = -1;
				for(int i=0; i<Instance.players.Count; i++)
				{
					if (((Player)Instance.players[i]).State==PlayerState.answering)
					{
						r = i;
						break;
					}
				}
				return r;
			}
		}
		
		public static ArrayList Winners
		{
			get 
			{
				ArrayList arrayList = new ArrayList();
				int max = 0;
				foreach(Player p in instance.players)
				{
					if (p.Score>max)
					{
						max = p.Score;
						arrayList.Clear();
						arrayList.Add(p);
					} else if (p.Score==max) {
						arrayList.Add(p);
					}
				}
				return arrayList;
			}
		}
		
		#endregion
		
		#region DynamicElements
		
		#region Variables
		private ArrayList players;
		#endregion
		
		#region Constructors
		private PlayerManager()
		{
			players = new ArrayList();
		}
		#endregion
		
		#endregion
	}
}
